require 'rubygems'
gem 'msgpack'
require 'msgpack'
gem 'msgpack-rpc'
require 'msgpack/rpc'

ipport = nil
if ARGV.size < 2
  puts "Usage: IndexClient IPAddress:Port Query..."
  exit 0
else
  ipport = ARGV[0]
end

ipports = ipport.split(/:/)
exit 0 if ipports.size < 2

cl = MessagePack::RPC::Client.new(ipports[0], ipports[1].to_i)
cl.timeout = 5

argpack = cl.call(:argument)
argtotal = argpack[0] + argpack[1] + argpack[2] + argpack[3]

if ARGV.size - 1 < argtotal
  puts "Short of query. " + argtotal.to_s + " needs but it has " + (ARGV.size - 1).to_s
  exit 0
end

varargs = ARGV[1, 1048576]
p varargs.size
argtotal = argpack[0] + argpack[1]

for i in argtotal..(argtotal+argpack[2]-1)
  varargs[i] = true if varargs[i] == 'true'
  varargs[i] = false if varargs[i] == 'false'
end
argtotal += argpack[2]

for i in argtotal..(argtotal+argpack[3]-1)
  varargs[i] = varargs[i].to_i
end

cl.call(:index, *varargs)
